package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.entity.vo.AttrGroupVO;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.pms.service.SkuAttrValueService;
import com.atguigu.gmall.pms.service.SpuAttrValueService;
import com.atguigu.gmall.pms.vo.ItemAttrValueVO;
import com.atguigu.gmall.pms.vo.ItemGroupVO;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {
    @Resource
    AttrService attrService;
    @Resource
    SpuAttrValueService spuAttrValueService;
    @Resource
    SkuAttrValueService skuAttrValueService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupVO> listGroupAndAttrsByCategoryId(Long categoryId) {
         /*
            1、根据分类id查询属性组
            2、根据查询到的每个属性组id查询组内的 属性列表(属性名  属性值列表)
            3、spu的是平台属性：type=1
         */
        List<AttrGroupEntity> attrGroupEntities = this.list(Wrappers.lambdaQuery(AttrGroupEntity.class)
                .eq(AttrGroupEntity::getCategoryId, categoryId));
        //streamApi: stream()将集合转为数据流，map将集合转为其他类型的结果集流，filter对数据流进行过滤
        //    collect 转为集合对象，reduce 对数据流数据进行计算
        return attrGroupEntities.stream().map(attrGroupEntity -> {
            AttrGroupVO attrGroupVO = new AttrGroupVO();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupVO);
            List<AttrEntity> attrEntities = attrService.list(Wrappers.lambdaQuery(AttrEntity.class)
                    .eq(AttrEntity::getGroupId, attrGroupEntity.getId())
                    .eq(AttrEntity::getType, 1));
            attrGroupVO.setAttrEntities(attrEntities);
            return attrGroupVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ItemGroupVO> withattrsGroup(Long cid, Long spuId, Long skuId) {
        //课件方式1：先查询cid对应的所有属性组，遍历属性组 查询组内的spuId的属性和值  查询组内的skuId的属性和值组装
        List<AttrGroupEntity> attrGroupEntities = this.list(Wrappers.lambdaQuery(AttrGroupEntity.class)
                .eq(AttrGroupEntity::getCategoryId, cid));
        if (CollectionUtils.isEmpty(attrGroupEntities)) {
            return null;
        }
        List<ItemGroupVO> itemGroupVOS = attrGroupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVO itemGroupVO = new ItemGroupVO();
            itemGroupVO.setGroupName(attrGroupEntity.getName());
            //组内的属性和属性值:查询组内的所有属性
            List<Long> attrIds = attrService.list(Wrappers.lambdaQuery(AttrEntity.class)
                    .eq(AttrEntity::getGroupId, attrGroupEntity.getId())
                    .select(AttrEntity::getId)).stream().map(AttrEntity::getId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(attrIds)) {
                //查询spu_attr_value表中 如果 attrId再上面的attrEntities集合中的 属性和值对象
                List<SpuAttrValueEntity> spuAttrValueEntities = spuAttrValueService.list(Wrappers.lambdaQuery(SpuAttrValueEntity.class)
                        .eq(SpuAttrValueEntity::getSpuId, spuId)
                        .in(SpuAttrValueEntity::getAttrId, attrIds)
                        .select(SpuAttrValueEntity::getAttrId,
                                SpuAttrValueEntity::getAttrName,
                                SpuAttrValueEntity::getAttrValue));
                //将spu中查询到的当前分组的属性和值集合转换成功
                List<ItemAttrValueVO> attrValueVOS = spuAttrValueEntities.stream().map(spuAttrValueEntity -> {
                    ItemAttrValueVO itemAttrValueVO = new ItemAttrValueVO();
                    BeanUtils.copyProperties(spuAttrValueEntity ,itemAttrValueVO );
                    return itemAttrValueVO;
                }).collect(Collectors.toList());
                //将sku中查询到的当前分组的属性和值集合转换成功
                attrValueVOS.addAll(skuAttrValueService.list(Wrappers.lambdaQuery(SkuAttrValueEntity.class)
                        .eq(SkuAttrValueEntity::getSkuId, skuId)
                        .in(SkuAttrValueEntity::getAttrId, attrIds)
                        .select(SkuAttrValueEntity::getAttrId,
                                SkuAttrValueEntity::getAttrName,
                                SkuAttrValueEntity::getAttrValue))
                        .stream().map(skuAttrValueEntity -> {
                            ItemAttrValueVO itemAttrValueVO = new ItemAttrValueVO();
                            BeanUtils.copyProperties(skuAttrValueEntity, itemAttrValueVO);
                            return itemAttrValueVO;
                        }).collect(Collectors.toList()));

                itemGroupVO.setAttrValues(attrValueVOS); //List<ItemAttrValueVO>
            }
            return itemGroupVO;
        //排除组内属性为空的组
        }).filter(itemGroupVO -> !CollectionUtils.isEmpty(itemGroupVO.getAttrValues())).collect(Collectors.toList());

        return itemGroupVOS;
        //方式2：自定义sql
        //查询spu属性组 属性和值
        //查询sku属性组 属性和值

        //合并上面的两个结果集
        /*
        -- 查询属性分组标题 categoryId=225
        SELECT t1.*
        FROM pms_attr_group t1
        WHERE t1.`category_id` = 225;
        -- 查询属性组 每个组内的所有属性
        SELECT t1.`name`,t2.*
        FROM pms_attr_group t1
        JOIN pms_attr t2
        ON t1.`id` = t2.`group_id`
        WHERE t1.`category_id` = 225;
        -- 查询`pms_spu_attr_value`每个属性 当前商品的属性值
        SELECT t1.`name` group_name ,t3.`attr_id` , t3.`attr_name` , t3.`attr_value`
        FROM pms_attr_group t1
        JOIN pms_attr t2
        ON t1.`id` = t2.`group_id`
        JOIN pms_spu_attr_value t3
        ON t3.`attr_id` = t2.`id`
        WHERE t1.`category_id` = 225
        AND t3.`spu_id` = 7;
        -- 查询`pms_sku_attr_value`每个属性 当前商品的属性值
        SELECT t1.`name` group_name ,t3.`attr_id` , t3.`attr_name` , t3.`attr_value`
        FROM pms_attr_group t1
        JOIN pms_attr t2
        ON t1.`id` = t2.`group_id`
        JOIN pms_sku_attr_value t3
        ON t3.`attr_id` = t2.`id`
        WHERE t1.`category_id` = 225
        AND t3.`sku_id` = 1;

        -- 最后将spu/sku查询到的属性值 以及组内的属性和值 集合合并

         */

    }

}